import getOpenAi from "../../util/chatGpt";
import { OpenAIApi } from "openai";

export interface IChatArgs {
  api: keyof OpenAIApi; // 使用的模型类型
  args: any;
  options: any;
}

// 获取chatGPt的返回结果
const getChatGptAnswerStream = async ({
  api,
  args,
  options,
}: IChatArgs): Promise<IAnyObject> => {
  return new Promise((resolve, reject) => {
    getOpenAi()[api]({...args, stream: true}, { // 要加上stream才有流式返回
        ...(({...options} as any) || {}),
        responseType: "stream",
      } as never)
      .then((res: any) => {
        resolve(res);
      })
      .catch((error) => {
        reject(error);
      });
  });
};

export default getChatGptAnswerStream;
